<template>
  <!-- 面包屑 -->
  <Breadcrumb />
  <el-button type="primary" @click="setFlag">添加分类</el-button>
  <el-table :data="suitList.slice((currentPage4-1)*pageSize4,currentPage4*pageSize4)" style="width: 100%">
    <el-table-column prop="id" label="id" width="180" />
    <el-table-column prop="title" label="名称" width="180" />
    <el-table-column label="启用禁用" width="180">
      <template #default="scope">
        <el-switch
          :active-value="1"
          :inactive-value="2"
          v-model="scope.row.status"
          class="ml-2"
          @change="setStatus(scope.row.id, scope.row)"
        />
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button
          size="small"
          type="primary"
          link
          @click="handleEdit(scope.$index, scope.row)"
        >
          编辑
        </el-button>
        <el-popconfirm
          title="你确定要删除吗?"
          confirm-button-text="是"
          cancel-button-text="否"
          @confirm="confirmEvent(scope.row)"
          @cancel="cancelEvent"
        >
          <template #reference>
            <el-button size="small" type="danger" link>删除</el-button>
          </template>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页 -->
  <div class="demo-pagination-block">
    <el-pagination
      v-model:current-page="currentPage4"
      v-model:page-size="pageSize4"
      :small="small"
      :disabled="disabled"
      :background="background"
      layout=" prev, pager, next, jumper"
      :total="suitList.length"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
  <!-- 对话弹框 -->
  <el-dialog
    v-model="flag"
    :title="flags ? '添加分类' : '编辑分类'"
    :show-close="false"
    width="800"
  >
    <el-form
      style="max-width: 600px"
      :model="forms"
      label-width="auto"
      class="demo-ruleForm"
      :size="formSize"
      status-icon
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="forms.title" />
      </el-form-item>

      <el-form-item label="排序" prop="sort">
        <el-input v-model="forms.sort" />
      </el-form-item>

      <!-- <el-form-item label="是否禁用">
        <el-switch
          :active-value="1"
          :inactive-value="2"
          v-model="forms.status"
          class="ml-2"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button v-if="flags" type="primary" @click="submitForm(ruleFormRef)">
          确定
        </el-button>
        <el-button v-else type="primary" @click="updateForm(ruleFormRef)">
          确定
        </el-button>
        <el-button @click="resetForm(ruleFormRef)">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, toRefs, onMounted } from "vue";
import Breadcrumb from "../../components/bread/Breadcrumb.vue";
import {
  getSuitCategoryAPI,
  setStatusAPI,
  addCategoryApi,
  delCategoryAPI,
  getBackAPI,
  updateCategoryAPI
} from "../../api/suit";

// 分页
const currentPage4 = ref(1)
const pageSize4 = ref(5)
const small = ref(false)
const background = ref(false)
const disabled = ref(false)
const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`)
}
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`)
}

onMounted(async () => {
  getSuitListDate();
});

// 刷新列表
const render = () => {
  getSuitListDate();
};

const getSuitListDate = async () => {
  //获取列表数据
  let res = await getSuitCategoryAPI();
  suitList.value = Object.values(res.data.data);
  console.log(suitList.value);
};

const suitList = ref([]);

// 更改switch
const setStatus = async (id, val) => {
  await setStatusAPI(id, val);
  console.log(id);
};

const forms = ref({
  title: "",
  sort: "",
});
const flag = ref(false); //控制弹窗显隐
const flags = ref(true); //控制添加、修改
const oid = ref(0)

// 点击添加按钮
const setFlag = () => {
  flags.value = true; //控制标题等主题为添加
  flag.value = true; //控制弹窗显示
  reforms();
};
// 添加弹窗确定
const submitForm = async (formEl) => {
  flag.value = false; //控制弹窗隐藏
  let res = await addCategoryApi(forms.value);
  if (res) {
    ElMessage({
      message: "添加成功",
      type: "success",
    });
  }
  render();
};
// 点击编辑按钮
const handleEdit = async (index, row) => {
  flags.value = false; //控制标题等主题为修改
  // 回显数据
  let res = await getBackAPI(row.id);
  forms.value = res.data.data;
  oid.value = row.id
  flag.value = true; //控制弹窗显示
};
// 编辑弹窗确定
const updateForm = async (formEl) => {
  flag.value = false; //控制弹窗隐藏
  let res = await updateCategoryAPI(oid.value,forms.value)
  if (res) {
    ElMessage({
      message: "编辑成功",
      type: "success",
    });
  }
  render()
};
// 添加/编辑弹窗取消
const resetForm = (formEl) => {
  flag.value = false; //控制弹窗隐藏
};
// 删除套装列表
const confirmEvent = async (row) => {
  //点击确定删除
  let res = await delCategoryAPI(row.id);
  if (res) {
    ElMessage({
      message: "删除成功",
      type: "success",
    });
  }
  render();
};
const cancelEvent = () => {
  //点击取消不删除
  console.log("cancel!");
};
// 数据回显归空
const reforms = () => {
  forms.value = {
    title: "",
    sort: "",
  };
};

const tableData = ref([
  {
    id: 1,
    name: "套装1",
    status: true,
    sort: "233",
  },
  {
    id: 2,
    name: "套装2",
    status: true,
    sort: "996",
  },
  {
    id: 3,
    name: "套装3",
    status: true,
    sort: "334",
  },
  {
    id: 4,
    name: "套装4",
    status: true,
    sort: "456",
  },
  {
    id: 5,
    name: "套装5",
    status: true,
    sort: "233",
  },
]);
</script>
<style scoped lang="less">
</style>